D:\a\tools.proto\tools.proto\compiler\src\compiler\builder.rs
Line | Count | Source |
1 | | // Copyright (c) 2024, BlockProject 3D |
2 | | // |
3 | | // All rights reserved. |
4 | | // |
5 | | // Redistribution and use in source and binary forms, with or without modification, |
6 | | // are permitted provided that the following conditions are met: |
7 | | // |
8 | | // * Redistributions of source code must retain the above copyright notice, |
9 | | // this list of conditions and the following disclaimer. |
10 | | // * Redistributions in binary form must reproduce the above copyright notice, |
11 | | // this list of conditions and the following disclaimer in the documentation |
12 | | // and/or other materials provided with the distribution. |
13 | | // * Neither the name of BlockProject 3D nor the names of its contributors |
14 | | // may be used to endorse or promote products derived from this software |
15 | | // without specific prior written permission. |
16 | | // |
17 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
19 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
20 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
21 | | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 | | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 | | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
24 | | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
25 | | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
26 | | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | |
29 | | use crate::compiler::message::{Field, FieldType, HeaderField, SizeInfo}; |
30 | | use crate::model::protocol::{Description, Endianness}; |
31 | | |
32 | | pub struct FieldBuilder { |
33 | | name: String, |
34 | | optional: bool, |
35 | | endianness: Endianness, |
36 | | header: Option<HeaderField>, |
37 | | codec: Option<String>, |
38 | | size_info: SizeInfo, |
39 | | description: Option<Description> |
40 | | } |
41 | | |
42 | | impl FieldBuilder { |
43 | 60 | pub fn new(name: String, optional: bool, endianness: Endianness) -> Self { |
44 | 60 | Self { |
45 | 60 | name, |
46 | 60 | description: None, |
47 | 60 | endianness, |
48 | 60 | optional, |
49 | 60 | header: None, |
50 | 60 | codec: None, |
51 | 60 | size_info: SizeInfo { |
52 | 60 | is_element_dyn_sized: false, |
53 | 60 | is_dyn_sized: true |
54 | 60 | } |
55 | 60 | } |
56 | 60 | } |
57 | | |
58 | 0 | pub fn name(&self) -> &str { |
59 | 0 | &self.name |
60 | 0 | } |
61 | | |
62 | 29 | pub fn has_codec(&self) -> bool { |
63 | 29 | self.codec.is_some() |
64 | 29 | } |
65 | | |
66 | 60 | pub fn description(mut self, description: Option<Description>) -> Self { |
67 | 60 | self.description = description; |
68 | 60 | self |
69 | 60 | } |
70 | | |
71 | 60 | pub fn header(mut self, header: Option<HeaderField>) -> Self { |
72 | 60 | self.header = header; |
73 | 60 | self |
74 | 60 | } |
75 | | |
76 | 87 | pub fn codec(mut self, codec: Option<String>) -> Self { |
77 | 87 | self.codec = codec; |
78 | 87 | self |
79 | 87 | } |
80 | | |
81 | 9 | pub fn size_info(mut self, size_info: SizeInfo) -> Self { |
82 | 9 | self.size_info = size_info; |
83 | 9 | self |
84 | 9 | } |
85 | | |
86 | 17 | pub fn fixed_size(mut self) -> Self { |
87 | 17 | self.size_info = SizeInfo { |
88 | 17 | is_dyn_sized: false, |
89 | 17 | is_element_dyn_sized: false |
90 | 17 | }; |
91 | 17 | self |
92 | 17 | } |
93 | | |
94 | 9 | pub fn dynamic_size(mut self) -> Self { |
95 | 9 | self.size_info = SizeInfo { |
96 | 9 | is_dyn_sized: true, |
97 | 9 | is_element_dyn_sized: true |
98 | 9 | }; |
99 | 9 | self |
100 | 9 | } |
101 | | |
102 | 54 | pub fn build(self, ty: FieldType) -> Field { |
103 | 54 | Field { |
104 | 54 | name: self.name, |
105 | 54 | header: self.header, |
106 | 54 | ty, |
107 | 54 | optional: self.optional, |
108 | 54 | size: self.size_info, |
109 | 54 | endianness: self.endianness, |
110 | 54 | description: self.description, |
111 | 54 | codec: self.codec, |
112 | 54 | } |
113 | 54 | } |
114 | | } |